package com.example.learningnote;

import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class DialogActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_dialog);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }


    public void myClick(View v){
        if(v.getId() == R.id.dialog_button){
            showBasicDialog();
        } else if (v.getId() == R.id.dialog_button2) {
            MyDialog md = new MyDialog(this , R.style.mydialog);
            md.show();
        } else if (v.getId() == R.id.dialog_button3) {
            showPopupWindow(v);
        }
    }

    public void showBasicDialog (){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage("您确定要退出应用吗?");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                finish();
            }
        });
        builder.setNegativeButton("取消",null);
        builder.show();
    }

    public void showPopupWindow(View anchorView) {
        // 获取视图
        View contentView = LayoutInflater.from(this).inflate(R.layout.popup_layout, null);

        // 实例化 PopupWindow，宽高设置为 WRAP_CONTENT
        PopupWindow popupWindow = new PopupWindow(
                contentView,
                ViewGroup.LayoutParams.WRAP_CONTENT, // 宽度自适应内容
                ViewGroup.LayoutParams.WRAP_CONTENT, // 高度自适应内容
                true // 可聚焦
        );
        // 设置背景
        popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(this, R.color.white));
        // 是否响应外部的点击事件
        popupWindow.setOutsideTouchable(true);
        // 是否响应点击事件
        popupWindow.setTouchable(true);
        // 设置点击外部关闭
        popupWindow.setOutsideTouchable(true);
        // 设置动画
        popupWindow.setAnimationStyle(R.style.translate_anim);
        // 显示 PopupWindow 后两个参数设置偏移量
        popupWindow.showAsDropDown(anchorView , 100 , 10);
    }

}